//
//  ALBatteryView.m
//  ALBattery
//
//  Created by Andrea Mario Lufino on 06/08/13.
//  Copyright (c) 2013 Andrea Mario Lufino. All rights reserved.
//

#import "ALBatteryView.h"
#import "UIImage+RTTint.h"

@implementation UIDevice (Battery)

- (CGFloat)batteryLevelInPercentage {
    self.batteryMonitoringEnabled = YES;
    return self.batteryLevel * 100;
}

@end

/*! The max width of the view */
#define kMaxWidth 128

/*! The x coordinate of the fill view */
#define kBatteryFillX 11 * self.width / kMaxWidth
/*! The y coordinate of the fill view */
#define kBatteryFillY (self.width / 2) - ((self.height * kBatteryFillMaxHeight / kMaxWidth) / 2)
/*! The height of the fill view */
#define kBatteryFillHeight self.height * kBatteryFillMaxHeight / kMaxWidth
/*! The width of the fill view */
#define kBatteryFillWidth self.width * kBatteryFillMaxWidth / kMaxWidth
/*! The max width of the fill view */
#define kBatteryFillMaxWidth 104.0f
/*! The max height of the fill view */
#define kBatteryFillMaxHeight 58

/*! The max corner radius of the fill view */
#define kMaxCornerRadius 8
/*! The corner radius of the fill view, calculated basing on the max value */
#define kCornerRadius kMaxCornerRadius * self.width / kMaxWidth

/*! The value of one percent of battery fill, calculated basing on the view width */
#define kOnePercent kBatteryFillWidth / 100

//#define kBatteryFillcolor MTNovelChapterTittleNightText//[UIColor colorWithRed:112/255.0 green:128/255.0 blue:148/255.0 alpha:1.0]

@implementation ALBatteryView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // If the width and height values are bigger than 128
        // then set these values to 128 (max size of the image)
        if (frame.size.width > 128 || frame.size.height > 128) {
            self.width = kMaxWidth;
            self.height = kMaxWidth;
        }
        // If the view isn't a square raise an exception
        if (self.width != self.height)
            [NSException raise:@"ALBatteryView frame error" format:@"The width and the height values have to be the same"];
        // Create the image view containing the battery image and
        // add it to the view
        imageView = [[UIImageView alloc] init];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        [imageView setFrame:CGRectMake(0, 0, self.width, self.height)];
        [imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
        [self addSubview:imageView];
        // Create the view which will fill the battery
        _batteryFill = [[UIView alloc] init];
        [_batteryFill setFrame:CGRectMake(kBatteryFillX, kBatteryFillY, 20, kBatteryFillHeight)];
        [_batteryFill setBackgroundColor:[UIColor colorWithRed:112/255.0 green:128/255.0 blue:148/255.0 alpha:1.0]];
        [[_batteryFill layer] setCornerRadius:kCornerRadius];
        // Insert the fill view (batteryFill) as first subview,
        // so the battery image will not be covered
        [self insertSubview:_batteryFill atIndex:0];
    }
    return self;
}

#pragma mark - Battery level set

- (void)setBatteryLevelWithColor:(UIColor*)color forValue:(CGFloat)batteryLevel image:(UIImage*)image {
    
    CGFloat newWidth;
    CGFloat newBatteryLevel = batteryLevel;
    imageView.image = image;
    newWidth = kOnePercent * newBatteryLevel;
    _batteryFill.backgroundColor = color;
    _batteryFill.width = newWidth;
}

- (void)reload {
    // Reset the width of the fill view
    _batteryFill.width = 0.0f;
    // Set the fill of the view to redo the animation
//    [self setBatteryLevelWithAnimation:YES forValue:[UIDevice currentDevice].batteryLevelInPercentage inPercent:YES];
}

@end
